/* ----------------------------------------------------------------------------------------------------------------
 * AbstractRenderer
 * ----------------------------------------------------------------------------------------------------------------
 */
package pure.engine.renderer 
{
	import flash.display.BitmapData;
	import flash.filters.BitmapFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import pure.engine.core.VisibleCore;
	import pure.engine.entity.IEntity;
	import pure.engine.system.ns_despair;
	import pure.utils.DesN;
	
	use namespace ns_despair;
	
	/**
	 * 抽象渲染器
	 * 
	 * @author ＃Pure
	 */
public class Renderer extends VisibleCore implements IRenderer 
{
	

	
	//======================
	// Member
	//======================
	
	
	ns_despair var m_flags:int;
	
	ns_despair var m_alpha:Number = 1.0;
	
	ns_despair var m_scaleX:Number = 1.0;
	
	ns_despair var m_scaleY:Number = 1.0;
	
	ns_despair var m_scaleMax:Number = 1.0;

	ns_despair var m_alphaMask:BitmapData;
	


	
	//======================
	// Cached
	//======================

	ns_despair var m_alphaDirty:Boolean, m_transformDirty:Boolean, m_bufferDirty:Boolean, _ignoreAngle:Boolean;


	
	
	
	
	
	//======================
	// Property
	//======================
	
	
	// 透明度
	public function get alpha():Number { return m_alpha; }
	public function set alpha(value:Number):void 
	{
		if (m_alpha != value)
		{
			m_alpha = DesN.clamp(value, 0, 1);
			m_alphaDirty = true;
			m_bufferDirty = true;
		}
	}
	

	// 缩放X
	public function get scaleX():Number { return m_scaleX; }
	public function set scaleX(v:Number):void 
	{
		v = v < 0 ? 0 : v;
		if (m_scaleX != v)
		{
			m_scaleX = v;
			m_bufferDirty = true;
			_checkTransformDirty();
		}

	}
	
	
	// 缩放Y
	public function get scaleY():Number { return m_scaleY; }
	public function set scaleY(v:Number):void
	{ 
		v = v < 0 ? 0 : v;
		if (m_scaleY != v)
		{
			m_scaleY = v;
			m_bufferDirty = true;
			_checkTransformDirty();
		}

	}

	
	// 缩放Max
	public function get scaleMax():Number { return m_scaleMax; }
	public function set scaleMax(v:Number):void
	{ 
		if (m_scaleMax != v)
		{
			m_scaleMax = v;
			m_rangeDirty = true;
			m_bufferDirty = true;
		}
	}
	
	
	// 弧度
	override public function set angle(v:Number):void 
	{ 
		if (m_angle != v)
		{
			m_angle = v;
			_checkTransformDirty();
			m_bufferDirty = true;
		}
	}
	

	// 忽略弧度
	public function get ignoreAngle():Boolean { return _ignoreAngle; }
	public function set ignoreAngle(b:Boolean):void
	{
		_ignoreAngle = b;
	}
	

	
	
	
	
	/**
	 * 旋转
	 * 
	 * @param duration
	 * @param offset
	 */
	public function rotate(duration:Number, offset:Number):void
	{
		
	}
	
	/**
	 * 旋转至
	 * 
	 * @param duration       持续时间
	 * @param finalRotation  最终角度
	 * @param shortcut       是否按捷径旋转
	 */
	public function rotateTo(duration:Number, finalRotation:Number, shortcut:Boolean = true):void
	{
		
	}
	
	

	
	/* ----------------------------------------------------------------------------------------------------------------
	 * ★★★★★★★★★★★★★★★★★★★★★  private  ★★★★★★★★★★★★★★★★★★★★★
	 * ----------------------------------------------------------------------------------------------------------------
	 */
	
	
	
	
	ns_despair function get range():Number
	{
		return m_range * m_scaleMax; 
	}

	
	/**
	 * 获取残像数据
	 */
	ns_despair function get bitmapData():BitmapData 
	{
		var bmd:BitmapData;
		var i:int;
		
		if (m_numFilter)
		{
			bmd = _generateBitmapData().clone()
			for (i = 0; i < m_numFilter; i++)
			{
				bmd.applyFilter(bmd, bmd.rect, cachedZeroPoint, m_filterList[i]);
			}
			return bmd;
		}
		return _generateBitmapData();
	}
	
	
	/**
	 * 透明遮罩
	 */
	ns_despair function get alphaMask():BitmapData 
	{ 
		if (m_alphaDirty)
		{
			if (!m_alphaMask)
			{
				m_alphaMask = new BitmapData(this.bitmapData.width, this.bitmapData.height);
			}
			m_alphaMask.fillRect(m_alphaMask.rect, int(m_alpha * 255) << 24 | 0xFFFFFF);
			m_alphaDirty = false;
		}
		return m_alphaMask; 
	}
	
	
	
	/**
	 * 更新
	 * 
	 * @param elapsedT
	 */
	ns_despair function update(elapsedT:Number):void 
	{

	}
	
	
	/**
	 * 毁灭
	 */
	override ns_despair function destroy():void
	{
		super.destroy();
		
		if (m_alphaMask != null)
		{
			m_alphaMask.dispose();
			m_alphaMask = null;
		}
	}
	
	
	

	/**
	 * 是否有形变
	 */
	protected function _checkTransformDirty():void
	{
		m_transformDirty = m_angle != 0 || m_scaleX != 1 || m_scaleY != 1;
	}
	

	/**
	 * @inheritDoc
	 * 
	 */
	protected function _generateBitmapData():BitmapData
	{
		// OVERRIDE !!
		return null;
	}
	
	
	
}

}